# -*- coding: utf-8 -*-
"""
Created on Wed Dec  8 07:15:34 2021

@author: Administrator
"""

import math


def down_load_license(file,path):
    with open(file,'a') as f:
        command1 = 'acc KeyFileManagement installKeyFile\n'
        command2 = 'sftp://qujing@6.48.243.25{}\n'.format(path)
        command3 = 'Passwd#1234567\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_gateway(file,gw):
    with open(file,'a') as f:
        command1 = 'cr Transport=1\n'
        command2 = 'cr Transport=1,Router=S1_CU\n'
        command3 = 'cr Transport=1,Router=S1_CU,RouteTableIPv4Static=1\n'
        command4 = 'cr Transport=1,Router=S1_CU,RouteTableIPv4Static=1,Dst=1\n'
        command5 = '0.0.0.0/0\n'
        command6 = 'cr transport=1,Router=S1_CU,RouteTableIPv4Static=1,Dst=1,NextHop=1\n'
        command7 = '{}\n'.format(gw)
        command8 = 'false\n'
        command9 = 'NULL\n'
        command10 = '1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_ip(file,ip):
    with open(file,'a') as f:
        command1 = 'cr Transport=1,VlanPort=VLAN_S1_CU\n'
        command2 = 'EthernetPort=1\n'
        command3 = '50\n'
        command4 = '\n'
        command5 = 'cr Router=S1_CU,InterfaceIPv4=TN_B_S1_CU\n'
        command6 = 'Transport=1,VlanPort=VLAN_S1_CU\n'
        command7 = 'false\n'
        command8 = '\n'
        command9 = 'set Transport=1,VlanPort=VLAN_S1_CU$ userLabel VLAN Interface for S1_CU connection\n'
        command10 = 'cr Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'
        command11 = '{}\n'.format(ip)
        command12 = '0\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines(command11)
        f.writelines(command12)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_sctp(file):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,AdditionalCoreNetwork=1\n'
        command2 = 'mcc=460,mnc=1,mncLength=2\n'
        command3 = '\n'
        command4 = 'cr Transport=1,SctpEndpoint=S1_CU\n'
        command5 = 'Transport=1,Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'
        command6 = '36422\n'
        command7 = 'SctpProfile=1\n'
        command8 = '\n'
        command9 = 'set AdditionalCoreNetwork=1$ sctpS1Ref SctpEndpoint=S1_CU\n'
        command10 = 'set AdditionalCoreNetwork=1$ upS1IpAddressRef Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_mme(file):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,TermPointToMme=CUMME01\n'
        command2 = 'administrativeState 1\n'
        command3 = 'ipAddress1 10.100.4.160\n'
        command4 = 'ipAddress2 10.100.4.161\n'
        command5 = 'end\n\n'

        command6 = 'crn ENodeBFunction=1,TermPointToMme=CUMME02\n'
        command7 = 'administrativeState 1\n'
        command8 = 'ipAddress1 10.100.4.36\n'
        command9 = 'ipAddress2 10.100.4.37\n'
        command10 = 'end\n\n'

        command11 = 'crn ENodeBFunction=1,TermPointToMme=CUMME03\n'
        command12 = 'administrativeState 1\n'
        command13 = 'ipAddress1 10.100.4.98\n'
        command14 = 'ipAddress2 10.100.4.99\n'
        command15 = 'end\n\n'

        command16 = 'crn ENodeBFunction=1,TermPointToMme=CUMME04\n'
        command17 = 'administrativeState 1\n'
        command18 = 'ipAddress1 10.100.4.162\n'
        command19 = 'ipAddress2 10.100.4.163\n'
        command20 = 'end\n\n'

        command21 = 'set TermPointToMme=CUMME01$ additionalCnRef AdditionalCoreNetwork=1\n'
        command22 = 'set TermPointToMme=CUMME02$ additionalCnRef AdditionalCoreNetwork=1\n'
        command23 = 'set TermPointToMme=CUMME03$ additionalCnRef AdditionalCoreNetwork=1\n'
        command24 = 'set TermPointToMme=CUMME04$ additionalCnRef AdditionalCoreNetwork=1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines(command11)
        f.writelines(command12)
        f.writelines(command13)
        f.writelines(command14)
        f.writelines(command15)
        f.writelines(command16)
        f.writelines(command17)
        f.writelines(command18)
        f.writelines(command19)
        f.writelines(command20)
        f.writelines(command21)
        f.writelines(command22)
        f.writelines(command23)
        f.writelines(command24)
        f.writelines('\n')
        f.writelines('\n')


def active_feature(file):
    with open(file,'a') as f:
        command1 = 'set CXC4012095 featureState 1\n'
        command2 = 'set CXC4011823 featureState 1\n'
        command3 = 'set CXC4010960 featureState 1\n'
        command4 = 'set CXC4010956 featureState 1\n'
        command5 = 'set CXC4010616 featureState 1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines('\n')
        f.writelines('\n')


def add_two_plmn(file):
    with open(file,'a') as f:
        command1 = 'set1 EUtranCellFDD additionalPlmnList mcc=460,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2\n'
        f.writelines(command1)
        f.writelines('\n')
        f.writelines('\n')

def modity_tac(file, cell, tac):
    with open(file,'a') as f:
        command1 = 'Set EUtranCellFDD={cell} tac {tac}\n'.format(
            cell = cell,
            tac = tac,
        )
        f.writelines(command1)
        f.writelines('\n')


def add_external_enb(file,mnc,enb):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb} \n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )
        command2 = 'eNBId {}\n'.format(enb)
        command3 = 'eNodeBPlmnId mcc=460,mnc={},mncLength=2\n'.format(mnc)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_external_enb_ip(file,mnc,enb,ip):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},TermPointToENB={plmn}-{enb}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )
        command2 = 'administrativeState 0\n'
        command3 = 'ipAddress {}\n'.format(ip)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_external_cell(file,mnc,enb,cellid,freq,pci,tac):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},ExternalEUtranCellFDD={plmn}-{enb}-{cellid}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                )
        command2 = 'activePlmnList mcc=460,mnc={},mncLength=2\n'.format(mnc)
        command3 = 'eutranFrequencyRef ENodeBFunction=1,EUtraNetwork=1,EUtranFrequency={}\n'.format(freq)
        command4 = 'localCellId {}\n'.format(cellid)
        command5 = 'physicalLayerCellIdGroup {}\n'.format(math.floor(pci/3))
        command6 = 'physicalLayerSubCellId {}\n'.format(pci%3)
        command7 = 'tac {}\n'.format(tac)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_relations(file,cellname,mnc,enb,cellid,freq):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtranCellFDD={cellname},EUtranFreqRelation={freq},EUtranCellRelation={plmn}-{enb}-{cellid}\n'.format(
                cellname = cellname,
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                freq = freq,
                )
        command2 = 'neighborCellRef ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},ExternalEUtranCellFDD={plmn}-{enb}-{cellid}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                )

        f.writelines(command1)
        f.writelines(command2)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def active_external_enb(file,mnc,enb):
    with open(file,'a') as f:
        command1 = 'lset ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},TermPointToENB={plmn}-{enb} administrativeState 1\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )

        f.writelines(command1)
        f.writelines('\n')
        f.writelines('\n')

def run_share_parameters(file):
    with open(file,'a') as f:
        command1 = '''
            st EUtranCell UNLOCKED
            bl st_group
            set ENodeBFunction=1$ allowMocnCellLevelCommonTac True
            set ,UtranFreqRelation cellReselectionPriority 2
            set ,UtranFreqRelation csFallbackPrio 2
            set ,UtranFreqRelation mobilityActionCsfb 0
            set ,UeMeasControl=1$ ueMeasurementsActiveGERAN False
            set ,UtranFreqRelation allowedPlmnList mcc=460,mnc=01,mncLength=2
            set ,UtranFreqRelation anrMeasOn False
            set ,UtranFreqRelation connectedModeMobilityPrio -1
            $cellResePri[1850] = 4
            $cellResePri[1825] = 4
            $cellResePri[100] = 5
            $cellResePri[2452] = 3
            $cellResePri[2458] = 3

            mr MyCellFDD
            ma MyCellFDD ^EUtranCellFDD
            for $mo in MyCellFDD
              $cellName = ldn($mo)
              get $mo earfcndl$ > $arf
              set $cellName,EUtranFreqRelation=$arf$ allowedPlmnList mcc=460,mnc=01,mncLength=2;mcc=460,mnc=11,mncLength=2
              set $cellName,EUtranFreqRelation=$arf$ cellReselectionPriority $cellResePri[$arf]
              lset $cellName,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ cellReselectionPriority $cellResePri[$arf]
            done

            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ mdtMeasOn false
            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ anrMeasOn false
            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ qRxLevMin -128

            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ allowedPlmnList mcc=460,mnc=01,mncLength=2
            deb st_group
        '''
        for line in [x for x in command1.split('\n') if x!= '\n']:
            f.writelines(line.strip())
            f.writelines('\n')
            f.writelines('\n')


def add_gran_freq(file,freq):
    with open(file,'a') as f:
        num = 1 if freq == 900 else 2
        command1 = 'cr ENodeBFunction=1,GeraNetwork=1\n'
        command2 = 'cr ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup={}\n'.format(freq)
        command3 = '{}\n'.format(num)
        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('\n')


def add_gran_band(file,freq,band):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,GeraNetwork=1,GeranFrequency={freq}_{band}\n'.format(
            freq = freq,
            band = band,
        )
        command2 = '{}\n'.format(band)
        command3 = '0\n'
        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('\n')



def add_cell_gran_freq(file,freq,cellname):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,EUtranCellFDD={cellname},GeranFreqGroupRelation={freq}\n'.format(
            cellname = cellname,
            freq = freq,
        )
        command2 = 'GeraNetwork=1,GeranFreqGroup={}\n'.format(freq)
        command3 = '2\n'
        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('\n')


def create_flex_counter(file):
    with open(file,'a') as f:
        command = '''
        crn ENodeBFunction=1,PmFlexCounterFilter=plmn0
        plmnFilterMin 0
        plmnFilterMax 0
        plmnFilterEnabled true
        end

        crn ENodeBFunction=1,PmFlexCounterFilter=plmn1
        plmnFilterMin 1
        plmnFilterMax 1
        plmnFilterEnabled true
        end

        for $idx = 1 to 9
        crn ENodeBFunction=1,PmFlexCounterFilter=plmn0qci$idx
        plmnFilterMin 0
        plmnFilterMax 0
        plmnFilterEnabled true
        qciFilterMin $idx
        qciFilterMax $idx
        qciFilterEnabled true
        end

        crn ENodeBFunction=1,PmFlexCounterFilter=plmn1qci$idx
        plmnFilterMin 1
        plmnFilterMax 1
        plmnFilterEnabled true
        qciFilterMin $idx
        qciFilterMax $idx
        qciFilterEnabled true
        end

        done

        hget PmFlexCounterFilter plmnFilter|qciFilter
        hget SharedNetworkSupport|CXC4010960$|CsfbToGeranUtran|CXC4010956$|WcdmaSessionContinuity|CXC4010616$ featurestate$|licensestate$
        '''
        for line in command.strip().split('\n'):
            f.writelines(line.strip()+'\n')


def make_cv(file):
    with open(file,'a') as f:
        command1 = '$date = `date +%Y%m%d`'
        command2 = 'cvms $date_LTE_open_CU_share'
        f.writelines(command1 +'\n')
        f.writelines(command2 +'\n')
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')

def restart_node(file):
    with open(file,'a') as f:
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')

def restart_node(file):
    with open(file,'a') as f:
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')


def add_csfall_back2gran(file,freq,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('cr ENodeBFunction=1,EUtranCellFDD={cellname},GeranFreqGroupRelation={freq}'.format(cellname = cellname,freq = freq)+'\n')
            f.writelines('GeraNetwork=1,GeranFreqGroup={freq}'.format(freq = freq)+'\n')
            f.writelines('end' + '\n')
            f.writelines('\n')

def add_csfall_back2wcmda(file,freq,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('crn ENodeBFunction=1,EUtranCellFDD={cell_name},UtranFreqRelation={freq}'.format(cellname = cellname,freq = freq)+'\n')
            f.writelines('cellReselectionPriority 0'+'\n')
            f.writelines('connectedModeMobilityPrio -1'+'\n')
            f.writelines('csFallbackPrio 2'+'\n')
            f.writelines('csFallbackPrioEC 0'+'\n')
            f.writelines('mobilityAction 2'+'\n')
            f.writelines('mobilityActionCsfb 1'+'\n')
            f.writelines('utranFrequencyRef ENodeBFunction=1,UtraNetwork=1,UtranFrequency={}'.format(freq)+'\n')
            f.writelines('end' + '\n')
            f.writelines('\n')

